exports.index = function(req,res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    //任务流创建
    async.waterfall([

        //1.查询首页广告列表
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.searchBanner + "/b_blid/32",
                    method : 'GET'
                },
                function (err, data) {
                    console.log(data);
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.bannerData = data.data;

                    cb();
                }
            );

        },

        //1.查询首页分类
        /*
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.searchForumCategory + "/is_independent_category/1",
                    method : 'GET'
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.forumCategoryData = data.data;

                    cb();
                }
            );

        },
        */

        //2.查询活动列表
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.searchForumActivity + "/fa_is_show/1",
                    method : 'GET'
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.activityData = data.data;

                    cb();
                }
            );
        },

        //3.查询附近的机构
        /*
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_nearby_organization + "/lng/"+restfulParams.lng+"/lat/"+restfulParams.lat+"/skip/0/limit/4",
                    method : 'GET'
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.organizationData = data.data;

                    cb();
                }
            );

        },
        */

        //查询机构约课信息
        /*
        function(cb){

            var tasks = [];

            //循环节点列表
            resultData.organizationData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {

                    R.SEND_HTTP(
                        req,
                        {
                            url    : CORE_APIConfig.coreAPI.search_organization_course+"/oc_oid/"+item.o_id+"/oc_type/1/sort_order/oc_create_time-desc/skip/0/limit/1",
                            method : 'POST',
                            data : {
                                fields:["oc_id", "oc_title", "oc_title_image","oc_old_price", "oc_new_price", "oc_course_count", "oc_sales_volume"].join(",")
                            }
                        },
                        function (err, data) {
                           
                            item.organizationAboutCourseData = data.data;
        
                            cb();
                        }
                    );

                });

            });

            async.waterfall(tasks, cb);


        },
        */

        //4.查询机构活动
        function(cb){

            /*
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_organization_activity_info + "/skip/0/limit/" + config.publicConfig.LIMIT,
                    method : 'GET'
                },
                function (err, data) {

                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.organizationActivityData = data.data;

                    cb();
                }
            );
            */

        //    R.SEND_HTTP(

        //         req,
        //         {
        //             url    : CORE_APIConfig.coreAPI.search_organization_activity_near_info + "/lng/"+restfulParams.lng+"/lat/"+restfulParams.lat+"/skip/0/limit/20",
        //             method : 'POST',
        //             data : {
        //                 oa_is_home:1
        //             }
        //         },
        //         function (err, data) {

        //             //判断是否有错误信息
        //             if (err) {
        //                 return cb(err, data);
        //             }

        //             resultData.organizationActivityData = data.data;

        //             cb();
        //         }
        //     );

        R.SEND_HTTP(
            req,
            {
                url    : CORE_APIConfig.coreAPI.search_organization_activity_info + "/oa_type/5/oa_is_end/0/skip/0/limit/20/sort_order/oa_join_count-desc",
                method : 'POST',
                data : {
                    keywords : "oa_category_text-" + restfulParams.keywords
                }
            },
            function (err, data) {
            
                //console.log(data);

                //判断是否有错误信息
                if (err) {
                    return cb(err, data);
                }

                resultData.organizationActivityData = data.data;

                cb();
            }
        );

        },

        //计算和自己的距离
   
        function(cb){

            for(var i = 0;i<resultData.organizationActivityData.length;i++){

                var tempLng = resultData.organizationActivityData[i].o_longitude;
                var tempLat = resultData.organizationActivityData[i].o_latitude;
                var distance = (common.GetDistance(tempLat,tempLng,restfulParams.lat,restfulParams.lng)).toFixed(1);    
                resultData.organizationActivityData[i].distance = distance;

            }

            cb();

        },
    

        //查询活动的类型和扩展
        function(cb){

            var tasks = [];

            //循环节点列表
            resultData.organizationActivityData.forEach(function (item, i) {

            
                //创建每一个插入任务
                tasks.push(function (cb) {

                    //拼团
                    if(item.oa_type == 0){

                        //console.log("拼团");
                       
                        R.SEND_HTTP(
                            req,
                            {
                                url    : CORE_APIConfig.coreAPI.search_bulk_purchase_activity_config + "/bpac_oaid/" + item.oa_id,
                                method : 'POST',
                                data : {
                                    fields:["bpac_id","bpac_open_price"].join(",")
                                }
                            },
                            function (err, data) {

                                //console.log("拼团");
                                if(data.data.length > 0){
                                   
                                    item.price =  data.data[0].bpac_open_price;
                                }
                               

                                cb();
                            }
                        );
                        //投票
                    }else if(item.oa_type == 1){
                        //console.log("投票");
                        cb();
                        //砍价
                    }else if(item.oa_type == 2){
                        //console.log("砍价");
                        item.price = 0.0;
                        cb();
                        //海报
                    }else if(item.oa_type == 3){
                        //console.log("海报");
                        cb();
                    }else if(item.oa_type == 4){


                        //console.log("其他");
                        cb();

                        //促销
                    }else if(item.oa_type == 5){

                        //console.log("促销");
                        
                        R.SEND_HTTP(
                            req,
                            {
                                url    : CORE_APIConfig.coreAPI.search_discounts_activity_config + "/dac_oaid/" + item.oa_id,
                                method : 'POST',
                                data : {
                                    fields:["dac_id","dac_new_price"].join(",")
                                }
                            },
                            function (err, data) {

                                //console.log(data);

                                if(data.data.length > 0){
                                    item.price =  data.data[0].dac_new_price;
                                }
                                

                                cb();
                            }
                        );

                    }else if(item.oa_type == 6){
                        cb();
                    }

                });

            });

            async.waterfall(tasks, cb);

        }

        ],function(err, result){

            //返回最终拼接结果
            RES.response(res,true,resultData);

        }
    );



}